﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovablePlatform : MonoBehaviour
{
    // Start is called before the first frame update
    public float moveSpeed;//平台移动速度
    public float distance;//平台移动距离
    public Vector2 position;//平台初始位置
    public Vector2 direction;
    void Start()
    {
        moveSpeed = Random.Range(1,3);
        distance = Random.Range(2,5);
        position = transform.position;
        direction = new Vector2(-1,0);//向右
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    public void Move()
    {
        IsChange();
        transform.Translate(direction*moveSpeed*Time.deltaTime);
    }

    public void IsChange()
    {
        if (transform.position.x-position.x<0||transform.position.x-position.x>distance)
        {
            direction *= new Vector2(-1,0);
            transform.Translate( direction * moveSpeed/10);
        }
    }
}
